package pl.edu.agh.student.bik.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import pl.edu.agh.student.logic.bik.BikReport;

public class BikService {
	private Map<String, BikReport> bikReportMap = new HashMap<String, BikReport>();

	public static final int MAX_OBLIGATIONS_GENERATOR = 10;
	public static final int MIN_OBLIGATIONS_GENERATOR = 1;
	public static final String DATE_FORMAT = "yyyy/MM";

	private static final String[] currency = { "PLN", "CHF", "EUR", "USD" };
	private static final String[] bank = { "PKO BP", "Bank BGŻ", "mBank",
			"Inteligo", "ING Bank Śląski", "Raiffeisen Bank", "Alior Bank",
			"Deutsche Bank PBC", "Citi Handlowy", "Bank Millennium",
			"Toyota Bank", "Volkswagen Bank direct", "Bank Zachodni WBK",
			"MultiBank", "Bank Pocztowy", "LUKAS Bank" };

	public void setReport(String pesel, BikReport report) {
		bikReportMap.put(pesel, report);
	}

	public BikReport getReport(String pesel) {
		System.out.println("BikService.getReport(): Customer pesel=" + pesel);
		BikReport r;
		if (bikReportMap.containsKey(pesel))
			r = bikReportMap.get(pesel);
		else {
			r = new BikReport();
			Random rand = new Random();
			Calendar cal;
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
			String[] obligations = new String[MIN_OBLIGATIONS_GENERATOR
					+ rand.nextInt(MAX_OBLIGATIONS_GENERATOR
							- MIN_OBLIGATIONS_GENERATOR + 1)];
			int i;
			for (i = 0; i < obligations.length; i++) {
				cal = Calendar.getInstance();
				cal.add(Calendar.YEAR, -rand.nextInt(20));
				cal.add(Calendar.MONTH, -rand.nextInt(12));
				cal.add(Calendar.DAY_OF_MONTH, -rand.nextInt(31));
				obligations[i] = bank[rand.nextInt(bank.length)] + ",";
				obligations[i] += sdf.format(cal.getTime()) + ",";
				obligations[i] += 1000.0 + 100000.0 * rand.nextDouble() + ",";
				obligations[i] += currency[rand.nextInt(currency.length)];
			}
			r.setObligations(obligations);
			bikReportMap.put(pesel, r);
		}
		return r;
	}
}
